<!doctype html>  
<html>  
   <head>  
    <meta charset="UTF-8">  
    <title></title>  
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />  
    <link href="../css/mui.min.css" rel="stylesheet" />  
    <script src="../js/mui.min.js"></script>  
    <style type="text/css">  
        #bcid{  
            width: 100%;  
            height: 100%;  
            position: absolute;  
            background: #000000;  
        }  
        html, body ,div{  
            height:100%;  
            width: 100%;  
        }  
        .fbt{  
            color: #0E76E1;  
            width: 50%;  
            background-color: #ffffff;  
            float: left;   
            line-height: 44px;  
            text-align: center;  
        }  
    </style>  
   </head>  
  <body>  
    <header class="mui-bar mui-bar-nav" style="background-color: #ffffff;">  
      <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>  
      <h1 class="mui-title" style="color: #0E76E1;">签到</h1>  
      <span class="mui-icon mui-icon-spinner-cycle mui-spin mui-pull-right" id="turnTheLight"></span>  
    </header>  
    <div id="bcid">     
         <!--盛放扫描控件的div-->         
    </div>  
 
    <script type="text/javascript">  
        var scan = null;//扫描对象
		var cID;
		var uID;
        mui.plusReady(function () {
			cID = plus.webview.currentWebview().cID;
			uID = plus.storage.getItem('uID');
            mui.init();  
          	startRecognize();  
        });  
        
        function startRecognize(){
           	try{
              	var filter;  
             	//自定义的扫描控件样式  
             	var styles = {frameColor: "#29E52C",scanbarColor: "#29E52C",background: ""}  
	            //扫描控件构造  
	            scan = new plus.barcode.Barcode('bcid',filter,styles);  
	            scan.onmarked = onmarked;   
	            scan.onerror = onerror;  
	            scan.start();  
	            //打开关闭闪光灯处理  
	            var flag = false;  
	            document.getElementById("turnTheLight").addEventListener('tap',function(){  
	               if(flag == false){  
	                  scan.setFlash(true);  
	                  flag = true;  
	               }else{  
	                 scan.setFlash(false);  
	                 flag = false;  
	               }  
	            });  
	          }catch(e){  
	            mui.toast("出错了"+e);
	          }  
            }
          
            function onerror(e){
                alert(e);
            }
            
			//识别出来结果,也就是6位密钥
            function onmarked(type, result) {
				mui.toast("正在识别中... ...");
				console.log(cID);
				console.log(uID);
				console.log(result);
            	mui.ajax({
					url: 'http://120.27.247.180:8080/School/courseCipher/sign',
					type:"post",
					data: {"cID": cID, "uID": uID, "pass":result},
					dataType: "jsonp",
					contentType: "application/x-www-form-urlencoded; charset=utf-8",
					jsonp: "jsoncallback",
					success: function(data){
						data = JSON.parse(data);
						if(data['suc']){
							mui.toast("签到成功");
							setTimeout(function(){
								var self = plus.webview.currentWebview();
								self.close();
							},1500);
						}else{
							mui.toast(data['msg']);
						}
					},
					error: function(){
						mui.toast("服务器异常，请稍后再试");
					}
				});
            }	

        </script>  
    </body>
    
</html